//
//  PpAltitudinalViewController.swift
//  Palmpilot
//
//  Created by 杨伟毅 on 2018/10/20.
//  Copyright © 2018年 杨伟毅. All rights reserved.
//

import UIKit
import CoreLocation
import CoreMotion
import Contacts
import QorumLogs


class PpAltitudinalViewController: PpBaseViewController,CLLocationManagerDelegate {
     lazy var altimer = CMAltimeter()
    @IBOutlet weak var altitudeLabel: UILabel!
    @IBOutlet weak var locallabel: UILabel!
    @IBOutlet weak var latAndLogLabel: UILabel!
    
    @IBOutlet weak var barometerLabel: UILabel!
    
    let locationManger = CLLocationManager()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.title = PpLanguageSwitchTool.getString(key: "Elevation of the instrument")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        creatLocationManage()
        if CMAltimeter.isRelativeAltitudeAvailable() {
            altimer.startRelativeAltitudeUpdates(to:OperationQueue.main) { (altitudeData, error) in
                if error == nil{
                    self.barometerLabel.text =  PpLanguageSwitchTool.getString(key: "The current air pressure") + String.init(format:"%.4f", (altitudeData?.pressure.floatValue)!) + "kPa"
                }else{
                    QL1("打印错误状态\(error!)")
                }
            }
        }
        // Do any additional setup after loading the view.
    }
    func creatLocationManage() -> Void {
        locationManger.delegate = self
        locationManger.distanceFilter = 0
        locationManger.desiredAccuracy = kCLLocationAccuracyBest
        locationManger.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManger.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currlocation = locations.last
        altitudeLabel.text = PpLanguageSwitchTool.getString(key: "The altitude") + String(format: "%3.2f", (currlocation?.altitude)!) + PpLanguageSwitchTool.getString(key: "mi")
        // currlocation?.coordinate.latitude
        latAndLogLabel.text = PpLanguageSwitchTool.getString(key: "Latitude and longitude") + String(format: "\n%.6f\n", (currlocation?.coordinate.latitude)!) +  String(format: "%.6f\n", (currlocation?.coordinate.longitude)!)
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(currlocation!) { (placemakes, error) in
            
            guard let number = placemakes?.count,number > 0 else{
                return
            }
//            if (placemakes?.count)! > 0{
                let placemark = placemakes?.first
                let addresDict = placemark?.addressDictionary
                let street = addresDict?["FormattedAddressLines"] as! [String]
                if let str =  ppUserder.object(forKey: "UserLanguage") as? String,str == "en" {
                    let mString = NSMutableString(string: street.first!)
                    CFStringTransform(mString, nil, kCFStringTransformToLatin, false)
                    self.locallabel.text = mString as String
                }else{
                    self.locallabel.text = street.first
                }
//            }
        }
  }
    func transformToPinYin(str:String)->String{
        let   mutableString =  str
        CFStringTransform(mutableString as! CFMutableString, nil, kCFStringTransformMandarinLatin, false)
//        CFStringTransform(str as! CFMutableString, nil, kCFStringTransformToLatin, false)
//        CFStringTransform(str as! CFMutableString, nil, kCFStringTransformStripDiacritics, false)
        return mutableString
    }
    
    deinit {
        
//        locationManger.stopUpdatingLocation()
        
    }
    

    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
